package com.gm.lib.utils;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;
import com.gm.common.utils.ResUtil;
import com.gm.lib.R;

/**
 * Created by cx on 15/4/21.
 */
public class GMAlertDialogUtil {


    private static AlertDialog mAlertDlg;


    public interface IOnAlertButtonClickedListener {
        public void onSureClick();

        public void onCancelClick();
    }


    public static Dialog showAlertDialog(Context context, int messageRes) {
        return showAlertDialog(context, context.getString(messageRes));
    }


    public static void showAlertDialog(Context con, String message, final IOnAlertButtonClickedListener listener) {

        showAlertDialog(con, ResUtil.getString(android.R.string.dialog_alert_title), message, listener);

    }


    public static Dialog showAlertDialog(Context con, String title, String message,
                                         final IOnAlertButtonClickedListener listener) {

        AlertDialog.Builder ad = new AlertDialog.Builder(con);
        View v = LayoutInflater.from(con).inflate(R.layout.dialog, null);
        Button cancle = (Button) v.findViewById(R.id.bt_cancle);
        Button commit = (Button) v.findViewById(R.id.bt_commit);
        TextView msg = (TextView) v.findViewById(R.id.tv_message);
        msg.setText(message);
        ad.setView(v);
        commit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cancelAlertDialog();
                listener.onSureClick();
            }
        });
        cancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cancelAlertDialog();
                listener.onCancelClick();
            }
        });
        mAlertDlg = ad.create();
        mAlertDlg.show();
        mAlertDlg.getWindow().setLayout(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT);
        mAlertDlg.getWindow().setGravity(Gravity.CENTER);
        return mAlertDlg;

    }


    public static Dialog showAlertDialog(Context context, String message) {

        mAlertDlg = new AlertDialog.Builder(context)
                .setTitle("提示")

                .setMessage(message)
                .setPositiveButton("确定", new Dialog.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        mAlertDlg.cancel();
                    }
                })
                .create();
        mAlertDlg.setIcon(android.R.drawable.ic_dialog_alert);
        mAlertDlg.show();

        return mAlertDlg;
    }


    public static Dialog showAlertDialog(Context context, String message, Dialog.OnClickListener listener) {

        mAlertDlg = new AlertDialog.Builder(context)
                .setTitle("提示")

                .setMessage(message)

                .setPositiveButton("确定", new Dialog.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        mAlertDlg.cancel();
                    }
                })
                .create();
        mAlertDlg.setIcon(android.R.drawable.ic_dialog_alert);
        mAlertDlg.show();

        return mAlertDlg;
    }


    public static void cancelAlertDialog() {
        if (mAlertDlg != null) {
            mAlertDlg.cancel();
            mAlertDlg = null;
        }
    }
}
